package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.li.utils.TransformUtil;
import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.TaskInstall;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/9/25 0025
 * Description: 维修员-》首装任务+加装任务
 */

public class TaskInstallAdapter extends RecyclerView.Adapter<TaskInstallAdapter.TaskInstallHolder> {


    public void setTaskInstallList(List<TaskInstall> taskInstallList) {
        mTaskInstallList = taskInstallList;
        notifyDataSetChanged();
    }

    private OnOpListener mOnOpListener;
    private String mStatus;
    private List<TaskInstall> mTaskInstallList;
    private LayoutInflater mInflater;

    public TaskInstallAdapter(Context context, String status) {
        mInflater = LayoutInflater.from(context);
        mStatus = status;
    }

    @Override
    public TaskInstallHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_task_install, parent, false);
        return new TaskInstallHolder(view);
    }

    @Override
    public void onBindViewHolder(TaskInstallHolder holder, int position) {
        TaskInstall install = mTaskInstallList.get(position);
        final int pos=position;
        String task;
        if (install.getType().equals("1")) {
            task = "首装"+install.getNumber()+"套";
        } else {
            task = "加装" + install.getNumber() + "套";
        }
        holder.mTxtTask.setTxtRight(task);
        holder.mTxtShop.setTxtRight(install.getShop_name());
        holder.mTxtPhone.setTxtRight(install.getMobile());
        holder.mTxtAddress.setTxtRight(install.getAddress());
        holder.mTxtTime.setTxtRight(TransformUtil.timeFormat(install.getAssign_time()));
        switch (install.getStatus()) {
            case "0":
                holder.mTxtAccept.setVisibility(View.VISIBLE);
                holder.mTxtAccept.setText("接受任务");
                holder.mTxtTaskStatus.setTxtRight("未接受");
                holder.mTxtAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnOpListener.onAccept(v, pos);
                    }
                });
                break;
            case "1":
                holder.mTxtAccept.setText("去完成");
                holder.mTxtAccept.setVisibility(View.VISIBLE);
                holder.mTxtTaskStatus.setTxtRight("已接受");
                holder.mTxtAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnOpListener.onDone(v, pos);
                    }
                });
                break;
            case "2":
                holder.mTxtAccept.setVisibility(View.GONE);
                holder.mTxtTaskStatus.setTxtRight("已完成");
                break;
        }
        holder.mTxtCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnOpListener.onCall(v, pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTaskInstallList == null ? 0 : mTaskInstallList.size();
    }

    public void setOnOpListener(OnOpListener onOpListener) {
        mOnOpListener = onOpListener;
    }

    static class TaskInstallHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_task)
        DoubleTextView mTxtTask;
        @BindView(R.id.txt_shop)
        DoubleTextView mTxtShop;
        @BindView(R.id.txt_phone)
        DoubleTextView mTxtPhone;
        @BindView(R.id.txt_address)
        DoubleTextView mTxtAddress;
        @BindView(R.id.txt_time)
        DoubleTextView mTxtTime;
        @BindView(R.id.txt_task_status)
        DoubleTextView mTxtTaskStatus;
        @BindView(R.id.txt_call)
        TextView mTxtCall;
        @BindView(R.id.txt_accept)
        TextView mTxtAccept;

        public TaskInstallHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnOpListener {
        void onCall(View view, int pos);

        void onAccept(View view, int pos);

        void onDone(View view, int pos);
    }
}
